package com.xianbeiying.app.ckgl.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.loopj.android.http.TextHttpResponseHandler;
import com.xianbeiying.app.ckgl.MyApplication;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.bean.MyCollectBean;
import com.xianbeiying.app.ckgl.bean.UrlBean;
import com.xianbeiying.app.ckgl.http.HttpPostManger;
import com.xianbeiying.app.ckgl.util.LogUtils;

import cz.msebera.android.httpclient.Header;

/**
 * 我的收藏详情
 * */
public class MyCollectDetailActivity extends BaseActivity {
    private ImageView iv_title_left;
    private TextView tv_title;
    private WebView webView;
    private MyCollectBean.VBean.COLLECTIONSBean collectBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_collect_detail);
        init();
    }

    private void init() {
        webView = (WebView) findViewById(R.id.webView);
        iv_title_left = (ImageView) findViewById(R.id.iv_title_left);
        tv_title = (TextView) findViewById(R.id.tv_title);
        initData();
    }

    private void initData() {
        tv_title.setText("收藏详情");
        iv_title_left.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        sethtml();

    }
    private  void getContent(String url,final WebView mmwebView){
        HttpPostManger.get(url,new TextHttpResponseHandler() {
            //获取成功
            @Override
            public void onSuccess(int i, Header[] headers, String s){
                LogUtils.e("精选","加载成功"+i+"======");
                mmwebView.getSettings().setDomStorageEnabled(true);//有可能是DOM储存API没有打开
                mmwebView.getSettings().setBlockNetworkImage(false);
                mmwebView.getSettings().setUseWideViewPort(false);
                mmwebView.getSettings().setJavaScriptEnabled(true);
                mmwebView.getSettings().setAllowFileAccess(true);
                mmwebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
                mmwebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
                mmwebView.getSettings().setLoadsImagesAutomatically(true);////支持自动加载图片
                mmwebView.loadDataWithBaseURL("http://"+ MyApplication.IP+":8080/", s, "text/html", "utf-8", null);
//                WebSettings webSettings = webView.getSettings();
//                //设置WebView属性，能够执行Javascript脚本
//                webSettings.setJavaScriptEnabled(true);
//                //设置可以访问文件
//                webSettings.setAllowFileAccess(true);
//                //设置支持缩放
//                webSettings.setBuiltInZoomControls(true);
//                //加载需要显示的网页
//                webView.loadUrl(s);
//                //设置Web视图
//                webView.setWebViewClient(new webViewClient ());
                LogUtils.e("精选","成功"+s+"======");
            }

            //获取失败
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                Log.e("精选", "加载失败");
            }
        });
    }
    //改写物理按键——返回的逻辑
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if(keyCode==KeyEvent.KEYCODE_BACK)
        {
            if(webView.canGoBack())
            {
                webView.goBack();//返回上一页面
                sethtml();
                return true;
            }
            else
            {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private void sethtml() {
        collectBean=(MyCollectBean.VBean.COLLECTIONSBean)getIntent().getSerializableExtra("collectBean");
        HttpPostManger.get("http://"+ MyApplication.IP+ UrlBean.JINGXUNDEFAUTL +collectBean.getDETAILHTML(), new TextHttpResponseHandler() {
            //获取成功
            @Override
            public void onSuccess(int i, Header[] headers, String s){
                LogUtils.e("技术支持","加载成功"+i+"======");
//                webView.getSettings().setJavaScriptEnabled(true);//是否支持动态加载js
//                webView.getSettings().setDefaultTextEncodingName("UTF-8");//编码方式
//                //以下两行代码是使网页能够自适屏
//                webView.getSettings().setUseWideViewPort(true);
//                webView.getSettings().setLoadWithOverviewMode(true);
//                webView.loadData(s, "text/html;charset=UTF-8", null);//加载网页
                webView.getSettings().setDomStorageEnabled(true);//有可能是DOM储存API没有打开
                webView.getSettings().setBlockNetworkImage(false);
                webView.getSettings().setUseWideViewPort(false);
                webView.getSettings().setJavaScriptEnabled(true);
                webView.getSettings().setAllowFileAccess(true);
                webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
                webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
                webView.getSettings().setLoadsImagesAutomatically(true);////支持自动加载图片
                webView.loadDataWithBaseURL("http://"+ MyApplication.IP+":8080/", s, "text/html", "utf-8", null);
                webView.setWebViewClient(new WebViewClient(){
                    @Override
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        // TODO Auto-generated method stub
                        LogUtils.e("ppppppp",url);
                        //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
//                        view.loadDataWithBaseURL("http://"+ MyApplication.IP+":8080/",url, "text/html", "utf-8", null);
                        getContent(url,view);
                        return true;
                    }
                });
            }

            //获取失败
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                Log.e("技术支持", "加载失败");
            }
        });
    }
}
